home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / libs / kpathsea / path-elt.c < prev    next >
C/C++ Source or Header  |  1995-06-25  |  3KB  |  134 lines

  1. /* path-elt.c: Return the stuff between colons.
  2.  
  3. Copyright (C) 1993 Karl Berry.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  */
  18.  
  19. #include <kpathsea/config.h>
  20.  
  21. #include <kpathsea/c-pathch.h>
  22. #include <kpathsea/pathsearch.h>
  23.  
  24.  
  25. /* The static (but dynamically allocated) area we return the answer in,
  26.    and how much we've currently allocated for it.  */
  27. static string elt = NULL;
  28. static unsigned elt_alloc = 0;
  29.  
  30. /* The path we're currently working on.  */
  31. static const_string path = NULL;
  32.  
  33.  
  34. /* Upon entry, the static `path' is at the first (and perhaps last)
  35.    character of the return value, or else NULL if we're at the end (or
  36.    haven't been called).  I make no provision for caching the results;
  37.    thus, we parse the same path over and over, on every lookup.  If that
  38.    turns out to be a significant lose, it can be fixed, but I'm guessing
  39.    disk accesses overwhelm everything else.  */
  40.  
  41. string
  42. kpse_path_element P1C(const_string, passed_path)
  43. {
  44.   const_string p;
  45.   string ret;
  46.   
  47.   if (passed_path)
  48.     path = passed_path;
  49.   
  50.   /* Check if called with NULL, and no previous path (perhaps we reached
  51.      the end).  */
  52.   else if (!path)
  53.     return NULL;
  54.   
  55.   /* OK, we have a non-null `path' if we get here.  */
  56.   assert (path);
  57.   p = path;
  58.   
  59.   /* Find the next colon (or the end of the path).  */
  60.   while (*p != 0 && !IS_ENV_SEP (*p))
  61.     p++;
  62.   
  63.   /* If there were no separators, return the whole thing this time, and
  64.      return NULL next time.  */
  65.   if (*p == 0)
  66.     {
  67.       ret = (string) path;
  68.       path = NULL;
  69.     }
  70.   
  71.   /* Otherwise, return the substring starting at `path'.  */
  72.   else
  73.     {
  74.       unsigned len = p - path;
  75.       
  76.       /* Make sure we have enough space (including the null byte).  */
  77.       if (len + 1 > elt_alloc)
  78.         {
  79.           elt_alloc = len + 1;
  80.           elt = xrealloc (elt, elt_alloc);
  81.         }
  82.  
  83.       strncpy (elt, path, len);
  84.       elt[len] = 0;
  85.       ret = elt;
  86.       
  87.       path += len + 1;
  88.     }
  89.  
  90.   return ret;
  91. }
  92.  
  93. #ifdef TEST
  94.  
  95. void
  96. print_path_elements (const_string path)
  97. {
  98.   string elt;
  99.   printf ("Elements of `%s':", path ? path : "(null)");
  100.   
  101.   for (elt = kpse_path_element (path); elt != NULL;
  102.        elt = kpse_path_element (NULL))
  103.     {
  104.       printf (" %s", *elt ? elt : "`'");
  105.     }
  106.   
  107.   puts (".");
  108. }
  109.  
  110. int
  111. main ()
  112. {
  113.   /* All lists end with NULL.  */
  114.   print_path_elements (NULL);    /* */
  115.   print_path_elements ("");    /* "" */
  116.   print_path_elements ("a");    /* a */
  117.   print_path_elements (":");    /* "", "" */
  118.   print_path_elements ("::");    /* "", "", "" */
  119.   print_path_elements ("a:");    /* a, "" */ 
  120.   print_path_elements (":b");    /* "", b */ 
  121.   print_path_elements ("a:b");    /* a, b */ 
  122.   
  123.   return 0;
  124. }
  125.  
  126. #endif /* TEST */
  127.  
  128.  
  129. /*
  130. Local variables:
  131. standalone-compile-command: "gcc -g -I. -I.. -DTEST path-elt.c kpathsea.a"
  132. End:
  133. */
  134.